@extends('common.admin_template')

@section('content')
    <div class="boxWidth">
        <h3>编辑{{$Tag->type == 'xcx'?'小程序':'标签'}}分类</h3>
        @include('common.notice_msg')
        <form method="post" action="{{url('admin/tag/save')}}">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <input type="hidden" name="Tag[id]" value="{{$Tag->id}}">
        <div class="row editFormItem">
            <div class="col-xs-3"><span>分类</span></div>
            <div class="col-xs-8" id="nameBox"><input type="text" class="form-control" id="name" name="Tag[name]" value="{{$Tag->name}}"><div class="addInputNotice">请输入分类名称</div></div>
        </div>
        <div class="row editFormItem">
            <div class="col-xs-3"><span>封面</span></div>
            <div class="col-xs-8" id="tagCoverBox">
                <div class="row"><div class="col-xs-11"><input type="text" class="form-control" id="TagCover" name="Tag[more]" value="{{$Tag->more}}"></div><div class="col-xs-1" id="coverImg"><img src="{{asset('img/upload.png')}}" alt=""></div></div>
                <div class="addInputNotice">请输入图片地址或上传图片</div>
            </div>
        </div>
        <div class="row editFormItem">
            <div class="col-xs-3"><span>排序</span></div>
            <div class="col-xs-8"><input type="text" class="form-control" name="Tag[order_id]" value="{{$Tag->order_id}}"></div>
        </div>
        <div style="{{$Tag->type != 'service'&&$Tag->type != 'couponCat'?'display:none':''}}">
            <div class="row editFormItem">
                <div class="col-xs-3"><span>筛选名称</span></div>
                <div class="col-xs-8"><input type="text" class="form-control" name="Tag[filter]" value="{{$Tag->filter}}">
                    <div class="addInputNotice filterCat" >
                        <span id="addForm">添加</span><span class="redColor" id="delForm">删除</span>（请输添加或删除筛选分类）
                        @for($i=0;$i<count($ArrId);$i++)
                            <div id="f_{{$i}}Box"><input type="text" id="f_{{$i}}" class="form-control" name="Tag[f_{{$i}}]" value="{{$ArrName[$i]}}"></div>
                        @endfor
                    </div>
                    @for($i=0;$i<count($ArrId);$i++)
                        <input style="display: none;" type="text" id="t_{{$i}}" class="form-control" name="Tag[t_{{$i}}]" value="{{$ArrId[$i]}}">
                    @endfor
                </div>
            </div>
        </div>
        <div class="row editFormItem">
            <div class="col-xs-3"><button class="btn btn-primary addInputBtn" type="submit" id="formSubmit">更新</button></div>
            <div class="col-xs-8"></div>
        </div>
        </form>
    </div>
@stop
@section('js')
    <script src="{{asset('js/plupload/plupload.full.min.js')}}"></script>
    <script>
        (function () {
            function hasError(id){
                var str =  $('#'+id).val().replace(/(^\s*)|(\s*$)/g, "");
                if(!str){
                    $('#'+id+'Box').addClass('has-error');
                    return 1;
                }
                return 0;
            }
            $('#formSubmit').on('click',function (e) {
                var sub = 0;
                if(!$('#name').val().replace(/(^\s*)|(\s*$)/g, "")){
                    $('#nameBox').addClass('has-error');
                    sub = 1
                }
                for(i=0;i<$('.filterCat input').length;i++){
                    if(hasError('f_'+i))sub=1;
                }
                if(sub){
                    e.preventDefault();
                }
            });

            $('#addForm').on('click',function () {
                var num = $('.filterCat input').length;
                $('.filterCat').append(
                        '<div id="f_'+num+'Box"><input type="text" id="f_'+num+'" class="form-control" name="Tag[f_'+num+']"></div>'
                );
            });
            $('#delForm').on('click',function () {
                var row = $('.filterCat input'),
                        num = row.length-1;
                if(num>=0){
                    row[num].remove();
                }else{
                    alert('没有可删除分类')
                }
            });

            var picName = new Date();
            picName = picName.getFullYear().toString()+((picName.getMonth()+1)<10?'0'+(picName.getMonth()+1):(picName.getMonth()+1))+(picName.getDate()<10?'0'+picName.getDate():picName.getDate().toString())+'_'+(picName.getHours()<10?'0'+picName.getHours():picName.getHours().toString())+(picName.getMinutes()<10?'0'+picName.getMinutes():picName.getMinutes().toString())+(picName.getSeconds()<10?'0'+picName.getSeconds():picName.getSeconds().toString());

            var uploader = new plupload.Uploader({
                browse_button : 'coverImg',
                runtimes : 'html5,flash,silverlight,html4',
                url : "{{asset('api/upload.php')}}",
                flash_swf_url : "{{asset('js/plupload/Moxie.swf')}}",
                silverlight_xap_url : "{{asset('js/plupload/Moxie.xap')}}",
                unique_names : true,
                init: {
                    PostInit: function() {},
                    BeforeUpload:function(){
                        uploader.setOption("multipart_params",{
                            'key': picName
                        });
                    },
                    QueueChanged:function(){
                        uploader.start();
                    },
                    FileUploaded:function(up, file, res){
                        if(res.response){
                            var msg = JSON.parse(res.response);
                            $('#TagCover').val("{{asset('upload')}}"+"/"+msg.name)
                        }
                    },
                    UploadComplete: function(up, files) {
                        console.log('complete')
                    },
                    Error : function(up, err) {
                        console.log('fail')
                    }
                }
            });
            uploader.init();
        })();
    </script>
@stop